﻿using System;
using System.IO;
using Microsoft.Win32;
using PMIS.BLL.System;

namespace PMIS.UI
{
	/// <summary>
	/// 登录界面
	/// </summary>
	public partial class LoginForm : BaseForm
	{
		#region 单例
		/// <summary>
		/// 本类的实例
		/// </summary>
		private static LoginForm _form = null;
		/// <summary>
		/// 用于本类实现单例的Locker
		/// </summary>
		private static object locker = new object();
		/// <summary>
		/// 构造函数
		/// </summary>
		private LoginForm()
		{
			InitializeComponent();
		}
		/// <summary>
		/// 获取本窗体的实例
		/// </summary>
		/// <returns></returns>
		public static LoginForm GetInstance()
		{
			if (_form == null || _form.IsDisposed)
			{
				lock (locker)
				{
					if (_form == null || _form.IsDisposed)
						_form = new LoginForm();
				}
			}

			return _form;
		}
		#endregion

		#region 窗体事件
		/// <summary>
		/// 窗体载入事件
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoginForm_Load(object sender, EventArgs e)
		{
			try
			{
				this.Focus();
				//	获取注册表中的路径
				string path = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\PMIS", "SQLitePath", string.Empty);
				if (string.IsNullOrEmpty(path))
				{
					ShowMessageBox("请选择SQLite数据文件的位置!");
					openFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
					if (openFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
					{
						path = openFileDialog.FileName;
						tbPassword.Select();
					}
				}

				tbPath.Text = path;
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion

		#region 触发事件
		/// <summary>
		/// 浏览
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btBrowser_Click(object sender, EventArgs e)
		{
			try
			{
				openFileDialog.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
				if (openFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
				{
					tbPath.Text = openFileDialog.FileName;
				}
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		/// <summary>
		/// 登录
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void btLogin_Click(object sender, EventArgs e)
		{
			try
			{
				if (string.IsNullOrEmpty(tbPath.Text.Trim()))
				{
					ShowErrorMessage("请选择SQLite数据文件路径!");
					return;
				}

				if (!File.Exists(tbPath.Text.Trim()))
				{
					ShowErrorMessage("指定路径的SQLite数据文件不存在!");
					return;
				}

				if (tbPassword.Text != "")
				{
					ShowErrorMessage("输入的密码不正确!");
					tbPassword.Select();
					return;
				}

				//	保存数据库路径
				BL_Registry.GetInstance().SetValue("SQLitePath", tbPath.Text.Trim());

				this.Hide();
				//	显示主窗体
				MainForm.GetInstance().Show(this);
			}
			catch (Exception ex)
			{
				ShowExceptionMessage(ex);
			}
		}
		#endregion
	}
}
